package com.oneday.ashop.webgate.book;

import com.oneday.ashop.core.base.mongo.BaseMongoDaoSupport;
import com.oneday.ashop.core.entity.CommonConstant;
import com.oneday.ashop.core.entity.Page;
import com.oneday.ashop.core.entity.User;
import com.oneday.ashop.core.entity.book.*;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 读书dao
 * User:bob.yang
 * Date 2015/8/14 0014
 */
@Service
public class BookDao extends BaseMongoDaoSupport {
    /**
     * 添加图书
     * @param book
     */
    public void addBook(Book book) {
        insert(book);
    }

    /**
     * 搜索热门书籍
     * @return
     */
    public List<Book> queryHotBooks() {
        Query query = new Query();
        includes(query, "_id", "name", "author", "image", "introduction",
                "publishDate", "publisher", "searchTimes");
        // 搜索热门书籍
        query.with(new Sort(Sort.Direction.DESC, "searchTimes")).limit(CommonConstant.HOT_BOOK_NUM);
        List<Book> bookList = find(query, Book.class);
        return bookList;
    }

    /**
     * 搜索推荐书籍
     * @return
     */
    public List<Book> queryRecommendBooks() {
        Query query = new Query();
        includes(query, "_id", "name", "author", "image", "introduction",
                "publishDate", "publisher", "searchTimes", "conmentNum");
        // 搜索推荐书籍
        query.with(new Sort(Sort.Direction.DESC, "conmentNum")).limit(CommonConstant.REC_BOOK_NUM);
        List<Book> bookList = find(query, Book.class);
        return bookList;
    }

    /**
     * 搜索所有书籍
     * @return
     */
    public List<Book> queryAllBooks() {
        Query query = new Query();
        includes(query, "_id", "name", "author", "image", "introduction",
                "publishDate", "publisher", "searchTimes", "conmentNum");
        List<Book> bookList = find(query, Book.class);
        return bookList;
    }

    /**
     * 搜索热门评论
     * @return
     */
    public List<Comment> queryHotComments() {
        Query query = new Query();
        includes(query, "_id", "userName", "content", "addTime", "replyNum",
                "bookID", "bookName", "replies", "replyNum");
        // 搜索热门评论
        query.with(new Sort(Sort.Direction.DESC, "replyNum")).limit(CommonConstant.HOT_COMMENTS_NUM);
        List<Comment> commentList = find(query, Comment.class);
        return commentList;
    }

    /**
     * 添加心情
     * @param mood
     */
    public void addMood(Mood mood) {
        insert(mood);
    }

    /**
     * 查找心情
     * @param uid
     * @return
     */
    public List<Mood> findMood(Integer uid) {
        Query query = Query.query(Criteria.where("uid").is(uid));
        query.with(new Sort(Sort.Direction.DESC, "addTime"));
        return find(query, Mood.class);
    }

    /**
     * 查找心情分页数据
     * @param page
     * @param fileds
     */
    public void findMoodPage(Page<Mood> page, String...fileds) {
        Query query = new Query(Criteria.where("status").nin(1,2));
        includes(query, fileds);
        query.with(new Sort(Sort.Direction.DESC, "addTime"));
        findEntityPage(Mood.class, page, query);
    }

    /**
     * 添加回应
     * @param comment
     */
    public void addComment(Comment comment) {
       insert(comment);
    }

    /**
     * 查询心情评论
     * @param mid
     * @return
     */
    public List<Comment> findComment(Integer mid, String...fields) {
        Query query = Query.query(Criteria.where("mid").is(mid));
        includes(query, fields);
        query.with(new Sort(Sort.Direction.ASC, "addTime"));
        return find(query, Comment.class);
    }

    /**
     *根据心情号查找心情
     * @param mid
     * @return
     */
    public Mood findMoodById( Integer mid, String...fields) {
        Query query = Query.query(Criteria.where("_id").is(mid));
        includes(query, fields);
        return findOne(query, Mood.class);
    }

    /**
     * 根据mid更新心情
     * @param mid
     */
    public void updateMood(Integer mid, Mood mood) {
        Query query = Query.query(Criteria.where("_id").is(mid));
        Update update = Update.update("praiseUids", mood.getPraiseUids());
        updateFirst(query, update, Mood.class);
    }

    /**
     * 更新心情的状态
     * @param mid
     * @param status
     */
    public void updateMoodById(Integer mid, Integer status) {
        Query query = Query.query(Criteria.where("_id").is(mid));
        Update update = Update.update("status", status);
        updateFirst(query, update, Mood.class);
    }

    /**
     * 添加日志
     * @param blog
     */
    public void addBlog(Blog blog) {
        insert(blog);
    }

    /**
     * 查找用户最新的日志
     * @param uid
     * @return
     */
    public Blog findBlog(Integer uid, String...fields) {
        Query query = new Query();
        query.addCriteria(Criteria.where("uid").is(uid));
        includes(query, fields);
        query.with(new Sort(Sort.Direction.DESC, "addTime"));
        return findOne(query, Blog.class);
    }

    /**
     * 根据用户号批量查找用户
     * @param uids
     * @param fields
     * @return
     */
    public List<User> findUsers(Set<Integer> uids, String...fields) {
        Query query = Query.query(Criteria.where("_id").in(uids));
        includes(query, fields);
        return find(query, User.class);
    }

    /**
     * 根据日志号查询日志
     * @param bid
     * @param fields
     * @return
     */
    public Blog findBlogById(Integer bid, String...fields) {
        Query query = Query.query(Criteria.where("_id").is(bid));
        includes(query, fields);
        return findOne(query, Blog.class);
    }

}
